﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Globalization;
using WTwitter.Model.Framework;
using WTwitter.Model.Framework.TextSplitter;
using WTwitter.Model.Twitter.Json;

namespace WTwitter.Model.Twitter {
	class DirectMessageItem : ITimelineItem {
		#region private member
		private DirectMessage _item;
		private string _text;
		private List<TextComponent> _textComponent;
		private string _detailDescription;
		private DateTime? _createdAt;
		private IUser _user;
		#endregion

		#region Constructor
		public DirectMessageItem(DirectMessage item) {
			_item = item;
			_user = new TwitterUser(item.Sender);
			_textComponent = Splitter.Split(HttpUtility.HtmlDecode(item.Text));
		}
		#endregion

		#region ITimelineItem メンバ

		public ItemType Type {
			get { return ItemType.DirectMessage; }
		}

		public string TypeOfSourceTimeline {
			get { return "DirectMessage"; }
		}

		public long Id {
			get { return _item.Id; }
		}

		public long? InReplyTo {
			get { return null; }
		}

		public string Text {
			get {
				if (string.IsNullOrEmpty(_text)) {
					_text = HttpUtility.HtmlDecode(_item.Text);
				}
				return _text; 
			}
		}

		public List<TextComponent> TextComponents {
			get {
				return _textComponent;
			}
		}

		public string DetailDescription {
			get {
				if (string.IsNullOrEmpty(_detailDescription)) {
					_detailDescription = string.Format(
						"Id: {1}{0}ScreenName: {2}{0}Name: {3}{0}CreatedAt: {4}{0}",
						Environment.NewLine,
						Id,
						User.ScreenName,
						User.Name,
						CreatedAtString,
						InReplyTo
						);
				}
				return _detailDescription;
			}
		}

		public DateTime CreatedAt {
			get {
				if (!_createdAt.HasValue) {
					_createdAt = DateTimeConverter.StringToDateTime(CreatedAtString);
				}
				return _createdAt.Value;
			}
		}

		public bool Favorited {
			get { return false; }
		}

		public IUser User {
			get { return _user; }
		}
		#endregion

		public string CreatedAtString {
			get { return _item.CreatedAtString; }
		}

		#region IEquatable<ITimelineItem> メンバ

		public bool Equals(ITimelineItem other) {
			return this.TypeOfSourceTimeline == other.TypeOfSourceTimeline && this.Id == other.Id;
		}

		#endregion
	}
}
